package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.config.mq.MqCommonUtil;
import com.egoo.ticket.server.dao.WorkOrderRelationMapper;
import com.egoo.ticket.server.pojo.dto.WorkOrderRelationDto;
import com.egoo.ticket.server.pojo.entity.WorkOrderRelation;
import com.egoo.ticket.server.service.WorkOrderRelationService;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author donny
 * @since 2020-04-23
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class WorkOrderRelationServiceImpl implements WorkOrderRelationService {
	@Autowired
	private WorkOrderRelationMapper workOrderRelationMapper;
	@Autowired
	private MqCommonUtil mqCommonUtil;
	/**
	 * @Author: Donny_dong
	 * @Description: 处理抄送人
	 * @Param:
	 * @return:
	 * @Date: 2020/4/30
	 */
	@Override
	public Integer updateWorkOrderRelation(WorkOrderRelationDto workOrderRelationDto) {
		String workOrderId = workOrderRelationDto.getWorkOrderId();
		WorkOrderRelation workOrderRelation = new WorkOrderRelation();
		workOrderRelation.setWorkOrderId(workOrderId);
		workOrderRelation = workOrderRelationMapper.selectWorkOrderRelation(workOrderRelation);
		String sender = workOrderRelation.getSender();
		if (StringUtils.isEmpty(sender)) {
			workOrderRelation.setSender(workOrderRelationDto.getSender());
		} else {
			sender = sender + "," + workOrderRelationDto.getSender();
			workOrderRelation.setSender(sender);
		}
		boolean isPubRedisTopic = true;

		Integer integer = workOrderRelationMapper.updateByNotPrimary(workOrderRelation);
		if (integer == null || integer <= 0) {
			throw new RuntimeException("更新工单关系表失败！");
		}
		if (isPubRedisTopic) {
			mqCommonUtil.pushToMsgTriggerQueue(workOrderId);
		}
		return integer;

	}


	@Override
	public DbResponse concatWorkOrder(WorkOrderRelationDto workOrderRelationDto) {
		String currentWorkOrderId = workOrderRelationDto.getCurrentWorkOrderId();
		String concatWorkOrderIds = workOrderRelationDto.getConcatWorkOrderIds();
		createCurrentWorkOrderRelation(currentWorkOrderId, concatWorkOrderIds);
		Integer integer = createConcatWorkOrderRelation(currentWorkOrderId, concatWorkOrderIds);
		if (integer < concatWorkOrderIds.split(",").length) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "失败！");
		}
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "成功！");
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 创建被关联关系
	 * @Param:
	 * @return:
	 * @Date: 2020/4/29
	 */
	private Integer createConcatWorkOrderRelation(String currentWorkOrderId, String concatWorkOrderIds) {

		List<String> ids = Arrays.asList(concatWorkOrderIds.split(","));
		Integer integer = 0;
		for (String s : ids) {
			integer += createCurrentWorkOrderRelation(s, currentWorkOrderId);

		}
		return integer;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 创建当前关联关系
	 * @Param:
	 * @return:
	 * @Date: 2020/4/29
	 */
	private Integer createCurrentWorkOrderRelation(String currentWorkOrderId, String concatWorkOrderIds) {
		WorkOrderRelation workOrderRelation = new WorkOrderRelation();
		workOrderRelation.setWorkOrderId(currentWorkOrderId);
		workOrderRelation = workOrderRelationMapper.selectWorkOrderRelation(workOrderRelation);
		String contactWorkOrderIdStr = workOrderRelation.getConcatWorkOrderIdStr();
		if (StringUtils.isEmpty(contactWorkOrderIdStr)) {
			workOrderRelation.setConcatWorkOrderIdStr(concatWorkOrderIds);
		} else {
			workOrderRelation.setConcatWorkOrderIdStr(contactWorkOrderIdStr + "," + concatWorkOrderIds);
		}

		return workOrderRelationMapper.updateByNotPrimary(workOrderRelation);
	}
}
